const resultConstant = {
    success: 1,
    error: 0,
    loginError: 2,
    date: 'date',
    code: 'code',
    msg: 'msg',
    tokenError: 'token非法'
};

const loginMsg = {
    loginSuccess: '登录成功',
    loginError: '登录失败',
    inputMail: '请输入邮箱',
    inputPassword: '请输入密码',
    inputCode: '请输入验证码',
    inputMailError: '邮箱格式不正确',
    inputPasswordError: '密码格式不正确'
};

// todo: 本地调试时，使用本地地址
const prefixUrl = '/api';
// const prefixUrl = 'http://localhost:8088';
// const prefixUrl = 'https://climbing-ewe-adjusted.ngrok-free.app/question';

const localStorageKey = {
    token: 'token',
    userInfo: 'userInfo'
};


new Vue({
    el: '#app',
    data: {
        // 登录表单
        loginForm: {
            mail: '',
            password: ''
        },
        // 注册表单
        registerForm: {
            mail: '',
            password: '',
            code: '',
            passwordAgain: ''
        },
        loading: false,
        // 注册中
        registering: false,
        // 倒计时剩余时间
        countdown: 0,
        // 是否正在进行倒计时
        isCountingDown: false,
        // 点击了发送-加载中
        loadingSend: false
    },
    methods: {
        // 消息提示
        myAlert(message) {
            this.$alert(message, '提示', {
                confirmButtonText: '确定',
                callback: action => {
                }
            });
        },
        // 登录
        login() {
            const mail = this.loginForm.mail;
            const password = this.loginForm.password;
            console.log("mail:" + mail + " password:" + password)

            if (mail === '') {
                this.myAlert(loginMsg.inputMail);
                return;
            }
            if (password === '') {
                this.myAlert(loginMsg.inputPassword);
                return;
            }

            //发送Ajax请求
            this.loading = true;
            const url = prefixUrl + "/user/user/login";
            axios.post(url, this.loginForm).then((resp) => {
                const result = resp.data;
                console.log("result=" + JSON.stringify(result))
                if (result.code === resultConstant.success) {
                    const token = result.data.token;
                    // 将token存入localStorage中
                    localStorage.setItem(localStorageKey.token, token);
                    // 将用户信息存入localStorage中
                    localStorage.setItem(localStorageKey.userInfo, result.data.userInfo);
                    this.$message({
                        type: 'success',
                        message: loginMsg.loginSuccess
                    });

                    const backToPreUrl = window.location.href.split("=");
                    if (backToPreUrl[1] != null) {
                        window.location.href = (backToPreUrl[1]);
                    } else {
                        window.location.href = "main.html";
                    }
                } else {
                    this.$message({
                        type: 'error',
                        message: result.msg
                    });
                }
            }).catch((error) => {
                this.loading = false;
                console.log(error);
            }).finally(() => {
                this.loading = false;
            });
        },
        // 注册
        register() {
            console.log(this.registerForm);
            // 判断两次输入的密码是否相同
            if (this.registerForm.password !== this.registerForm.passwordAgain) {
                this.myAlert('两次输入的密码不相同');
                return;
            }
            // 注册
            this.loading = true;
            const url = prefixUrl + "/user/user/register";
            const registerDTO = {
                mail: this.registerForm.mail,
                password: this.registerForm.password,
                code: this.registerForm.code
            };
            axios.post(url, registerDTO).then((resp) => {
                const result = resp.data;
                if (result.code === resultConstant.success) {
                    this.$message({
                        type: 'success',
                        message: '注册成功'
                    });
                    // 清空表单
                    this.registerForm = {
                        mail: '',
                        password: '',
                        code: '',
                        passwordAgain: ''
                    };
                    // 返回登录界面
                    // location.href = "login.html";
                } else {
                    this.$message({
                        type: 'error',
                        message: result.msg
                    });
                }
            }).catch((error) => {
                this.loading = false;
                console.log(error);
            }).finally(() => {
                this.loading = false;
            });
        },
        // 发送验证码到邮箱
        sendMail() {
            if (!this.isCountingDown) {
                // 发送验证码
                const url = prefixUrl + "/user/user/sendCode/" + this.registerForm.mail;
                const registerDTO = {
                    mail: this.registerForm.mail,
                    password: this.registerForm.password,
                    code: this.registerForm.code
                };
                this.loadingSend = true;
                axios.get(url).then((resp) => {
                    const result = resp.data;
                    if (result.code === resultConstant.success) {
                        this.$message({
                            type: 'success',
                            message: '验证码发送成功'
                        });
                        this.loadingSend = false;
                        // 开始倒计时
                        this.isCountingDown = true;
                        // 倒计时为 60 秒
                        this.countdown = 60;
                        this.startCountdown();
                    } else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        });
                    }
                }).catch((error) => {
                    console.log(error);
                }).finally(() => {
                    this.loadingSend = false;
                });
            }
        },
        // 开始倒计时
        startCountdown() {
            if (this.countdown > 0) {
                this.countdown--;
                setTimeout(() => {
                    this.startCountdown();
                }, 1000);
            } else {
                this.isCountingDown = false;
                this.countdown = 0;
                // 倒计时结束后可以做的操作
            }
        }
    }
});